﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppFabricSample.Data;
using Microsoft.ApplicationServer.Caching;
using System.Threading;


namespace AppFabricSample.Console
{
    class Program
    {
        static void Main(string[] args)
        {

            // DataCacheFactory will use settings from app.config
            using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
            {


                // Get a default cache client
                DataCache dataCache = dataCacheFactory.GetCache("TestCache");


                // Get the cached object back from the cache
                Student cachedPerson = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                //dataCache.ResetObjectTimeout("Ram", TimeSpan.FromSeconds(15));
                System.Console.WriteLine(cachedPerson.Name + ": " + cachedPerson.Marks.ToString());
                cachedPerson.Marks = 755;
                //dataCache.Remove("Ram");
                // dataCache.Add("Ram", cachedPerson, TimeSpan.FromSeconds(2));

                dataCache.Put(CacheKey<Student>.ForStudentByName("Ram"), cachedPerson);


                //SleepFor(10);

                Student cachedPerson1 = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                System.Console.WriteLine(cachedPerson1.Name + ": " + cachedPerson1.Marks.ToString());


                SleepForMin(3);
                Student cachedPerson2 = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                System.Console.WriteLine(cachedPerson2.Name + ": " + cachedPerson2.Marks.ToString());


            }
            System.Console.ReadKey();
        }
        static void SleepForSec(int sec)
        {
            System.Console.WriteLine("Sleeping for {0:00.02} SECONDS STARTED", sec.ToString());
            for (int i = 1; i <= sec; i++)
            {
                Thread.Sleep(1000);
                System.Console.WriteLine("DONE: {0:00.02} SECONDS", i.ToString());
            }
            System.Console.WriteLine("Sleeping for {0:00.02}  SECONDS END", sec.ToString());
        }

        static void SleepForMin(int min)
        {
            System.Console.WriteLine("Sleeping for {0:00.02} MIN STARTED", min.ToString());
            for (int i = 1; i <= (min); i++)
            {
                Thread.Sleep(1000 * 60);
                System.Console.WriteLine("DONE: {0:00.02} MIN", i.ToString());
            }
            System.Console.WriteLine("Sleeping for {0:00.02}  MIN END", min.ToString());
        }
    }


    
}
